import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

// import Users from '../components/users/Users.vue'
const Users = () => import(/* webpackChunkName: "users" */ '../components/users/Users.vue')

// import Roles from '../components/rights/Roles.vue'
const Roles = () => import(/* webpackChunkName: "roles_rights" */ '../components/rights/Roles.vue')
// import Rights from '../components/rights/Rights.vue'
const Rights = () => import(/* webpackChunkName: "roles_rights" */ '../components/rights/Rights.vue')

// import Categories from '../components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "cate_params_goods_addgoods" */ '../components/goods/Categories.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "cate_params_goods_addgoods" */ '../components/goods/Params.vue')
// import Goods from '../components/goods/Goods.vue'
const Goods = () => import(/* webpackChunkName: "cate_params_goods_addgoods" */ '../components/goods/Goods.vue')
// import AddGoods from '../components/goods/AddGoods.vue'
const AddGoods = () => import(/* webpackChunkName: "cate_params_goods_addgoods" */ '../components/goods/AddGoods.vue')

// import Orders from '../components/order/Orders.vue'
const Orders = () => import(/* webpackChunkName: "orders" */ '../components/order/Orders.vue')
// import Reports from '../components/reports/Reports.vue'
const Reports = () => import(/* webpackChunkName: "reports" */ '../components/reports/Reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: AddGoods },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 配置路由导航守卫
router.beforeEach((to, from, next) => {
  //  如果用户访问登录页面, 直接放行
  if (to.path === '/login') return next()
  // 获取保存的token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页面
  if (!tokenStr) return next('/login')
  next()
})

export default router
